-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
--[[

]]--
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
local assets =
{
    Asset("ANIM", "anim/loramia_building_swiftstrike_creation.zip"),
    Asset( "IMAGE", "images/inventoryimages/loramia_building_swiftstrike_creation.tex" ),
    Asset( "ATLAS", "images/inventoryimages/loramia_building_swiftstrike_creation.xml" ),
}

-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
--- 参数
    local TRAP_RADIUS = 2
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
--- 天赋检查
    local function IsDeployerUnlockedTalent(player)
        if player and player.replica.loramia_com_talent_tree and player.replica.loramia_com_talent_tree:IsUnlocked("evolution_rapid_strike") then
            return true
        end
        return false
    end
    local function IsTrapEvoluted(inst)
        return inst.components.loramia_data:Get("evoluted") or false
    end
    local function SetTrapEvoluted(inst,flag)
        inst.components.loramia_data:Set("evoluted",flag)
    end
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
--- 陷阱模块
    local function OnExplode(inst, target)  --- 触发陷阱
        inst.AnimState:PlayAnimation("up")
        inst.SoundEmitter:PlaySound("dontstarve/common/trap_teeth_trigger")
        if target and target:IsValid() and target.components.combat  then
            if not IsTrapEvoluted(inst) then
                target.components.combat:GetAttacked(inst,150)
            else
                target.components.combat:GetAttacked(inst,10)
                if target.components.freezable ~= nil then
                    target.components.freezable:AddColdness(1)
                    target.components.freezable:SpawnShatterFX()
                end
            end
        end
        -- if inst.components.finiteuses then
        --     inst.components.finiteuses:Use(1)
        -- end
    end

    local function OnReset(inst)   --- 重置陷阱为收缩状态
        -- if inst.components.inventoryitem ~= nil then
        --     inst.components.inventoryitem.nobounce = true
        -- end
        -- if not inst:IsInLimbo() then
        --     -- inst.MiniMapEntity:SetEnabled(true)
        -- end
        if not inst.AnimState:IsCurrentAnimation("down_loop") then
            inst.SoundEmitter:PlaySound("dontstarve/common/trap_teeth_reset")
            inst.AnimState:PlayAnimation("down")
            inst.AnimState:PushAnimation("down_loop", false)
        end
    end

    local function SetSprung(inst)  --- 保持弹出状态
        -- if inst.components.inventoryitem ~= nil then
        --     inst.components.inventoryitem.nobounce = true
        -- end
        -- if not inst:IsInLimbo() then
        --     -- inst.MiniMapEntity:SetEnabled(true)
        -- end
        inst.AnimState:PlayAnimation("up_loop")
    end

    local function OnDeactivateFn(inst) --- 保持收缩状态
        if inst.components.inventoryitem ~= nil then
            inst.components.inventoryitem.nobounce = false
        end
        -- inst.MiniMapEntity:SetEnabled(false)
        inst.AnimState:PlayAnimation("down_loop")
        -- inst.components.loramia_data:Set("is_spring",false) -- 记录伸出状态
    end
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
--- 电器模块安装
    local function task_fn(inst)        
        local evoluted = IsTrapEvoluted(inst) --- 天赋布置标记位
        --------------------------------------------------------------------------------------
        --- 充电执行
            if not inst.components.fueled:IsFull() and not evoluted then
                if inst.components.loramia_com_power_device:IsGotPowerSucceed(5) then
                    inst.components.fueled:DoDelta(5)
                end
            end
        --------------------------------------------------------------------------------------
        --- 伸出状态检查
            if not inst:HasTag("minesprung") then
                return
            end
        --------------------------------------------------------------------------------------
        --- 收回计时器 和 消耗
            inst.reset_watting_time = inst.reset_watting_time or 0
            local loop_time = 3 --- 3秒重置一次
            local fueled_cost = inst.components.fueled.maxfuel  --- 消耗燃料
            if evoluted then
                loop_time = 1
                fueled_cost = 0
            end
            if inst.components.fueled:IsFull() or evoluted then
                if inst.reset_watting_time >= loop_time then
                    inst.components.mine:Reset()
                    inst.components.fueled:DoDelta(-fueled_cost)
                    inst.reset_watting_time = 0
                else
                    inst.reset_watting_time = inst.reset_watting_time + 1
                end
                return
            end
        --------------------------------------------------------------------------------------
    end
    local function Device_Com_Install(inst)
        inst:AddComponent("loramia_com_power_device")
        inst.components.loramia_com_power_device:SetIndicator(true)
        inst.components.loramia_com_power_device:SetIndicatorRadius(TRAP_RADIUS)
        inst:DoPeriodicTask(1,task_fn)
    end
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
--- 创建陷阱
    local function fn()
        local inst = CreateEntity()

        inst.entity:AddTransform()
        inst.entity:AddAnimState()
        inst.entity:AddSoundEmitter()
        inst.entity:AddMiniMapEntity()
        inst.entity:AddNetwork()

        -- inst.entity:AddDynamicShadow()
        -- inst.DynamicShadow:SetSize(2, 1)

        MakeInventoryPhysics(inst)
        inst:SetDeploySmartRadius(DEPLOYSPACING_RADIUS[DEPLOYSPACING.LESS] / 2)

        inst.MiniMapEntity:SetIcon("loramia_building_sharpstrike_creation.tex")

        inst.AnimState:SetBank("loramia_building_swiftstrike_creation")
        inst.AnimState:SetBuild("loramia_building_swiftstrike_creation")
        inst.AnimState:PlayAnimation("item")

        inst:AddTag("trap")

        MakeInventoryFloatable(inst)

        inst.entity:SetPristine()

        if not TheWorld.ismastersim then
            return inst
        end
        ----------------------------------------------------------------------------------
        --- 数据
            inst:AddComponent("loramia_data")
        ----------------------------------------------------------------------------------
        --- 物品和检查
            inst:AddComponent("inspectable")
        ----------------------------------------------------------------------------------
        --- 陷阱模块
            inst:AddComponent("mine")
            inst.components.mine:SetRadius(TRAP_RADIUS)
            inst.components.mine:SetAlignment("player")
            inst.components.mine:SetOnExplodeFn(OnExplode)
            inst.components.mine:SetOnResetFn(OnReset)
            inst.components.mine:SetOnSprungFn(SetSprung)
            inst.components.mine:SetOnDeactivateFn(OnDeactivateFn)
            inst.components.mine:Reset()
        ----------------------------------------------------------------------------------
        --- 燃料模块
            inst:AddComponent("fueled")
            inst.components.fueled.maxfuel = 50
            inst.components.fueled:SetPercent(0)
        ----------------------------------------------------------------------------------
        --- 电器节点
            Device_Com_Install(inst)
        ----------------------------------------------------------------------------------
        --- 挖掘
            inst:AddComponent("workable")
            inst.components.workable:SetWorkAction(ACTIONS.DIG)
            inst.components.workable:SetWorkLeft(1)
            inst.components.workable:SetOnFinishCallback(function(inst,worker)
                if worker and worker:HasTag("player") then
                    -- inst:AddComponent("lootdropper")
                    -- local item = inst.components.lootdropper:SpawnLootPrefab("loramia_building_swiftstrike_creation_kit")
                    local item = SpawnPrefab("loramia_building_swiftstrike_creation_kit")
                    local save_record = inst:GetSaveRecord()
                    item.components.loramia_data:Set("record",save_record)
                    item.Transform:SetPosition(inst.Transform:GetWorldPosition())
                    Launch(item,worker,0.01)
                    inst:Remove()
                else
                    inst.components.workable:SetWorkLeft(1)
                end
            end)
        ----------------------------------------------------------------------------------
        return inst
    end
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
--- item    
    local function OnDropped(inst) --- 作为物品丢出来
    end
    local function ondeploy(inst, pt, deployer) -- 放置出来的时候
        local save_record = inst.components.loramia_data:Get("record")
        local trap = nil
        if save_record ~= nil then
            trap = SpawnSaveRecord(save_record)
        else
            trap = SpawnPrefab("loramia_building_swiftstrike_creation")
        end
        trap.Transform:SetPosition(pt.x,0,pt.z)
        trap.components.mine:Reset()
        --------------------------------------------------------------------------
        --- 天赋
            if IsDeployerUnlockedTalent(deployer) then
                SetTrapEvoluted(trap,true)
                trap.components.fueled:SetPercent(1)                
            else
                SetTrapEvoluted(trap,false)
            end
        --------------------------------------------------------------------------
        inst:Remove()
    end
    local function item_fn()
        local inst = CreateEntity()

        inst.entity:AddTransform()
        inst.entity:AddAnimState()
        inst.entity:AddSoundEmitter()
        inst.entity:AddMiniMapEntity()
        inst.entity:AddNetwork()

        -- inst.entity:AddDynamicShadow()
        -- inst.DynamicShadow:SetSize(2, 1)

        MakeInventoryPhysics(inst)
        inst:SetDeploySmartRadius(DEPLOYSPACING_RADIUS[DEPLOYSPACING.LESS] / 2)

        inst.MiniMapEntity:SetIcon("loramia_building_sharpstrike_creation.tex")

        inst.AnimState:SetBank("loramia_building_swiftstrike_creation")
        inst.AnimState:SetBuild("loramia_building_swiftstrike_creation")
        inst.AnimState:PlayAnimation("item")

        -- inst:AddTag("trap")
        inst:AddTag("deploykititem")


        MakeInventoryFloatable(inst)

        inst.entity:SetPristine()

        if not TheWorld.ismastersim then
            return inst
        end
        ----------------------------------------------------------------------------------
        --- 
            inst:AddComponent("loramia_data")
        ----------------------------------------------------------------------------------
        --- 物品和检查
            inst:AddComponent("inspectable")
            inst:AddComponent("inventoryitem")
            inst.components.inventoryitem:SetOnDroppedFn(OnDropped)
            inst.components.inventoryitem.imagename = "loramia_building_swiftstrike_creation_kit"
            inst.components.inventoryitem.atlasname = "images/inventoryimages/loramia_building_swiftstrike_creation_kit.xml"
            inst.components.inventoryitem:SetSinks(true)
        ----------------------------------------------------------------------------------
        --- 放置模块
            inst:AddComponent("deployable")
            inst.components.deployable.ondeploy = ondeploy
            inst.components.deployable:SetDeploySpacing(DEPLOYSPACING.LESS)
        ----------------------------------------------------------------------------------
        return inst
    end
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
--- 
    local function placer_postinit_fn(inst)
        -- TheWorld:PushEvent("loramia_com_power_battery_indicator_active",inst)
        -------------------------------------------------------------------
        --- 通告所有发电机
            local call_back = {
                in_battery_area = {}
            }
            TheWorld:PushEvent("loramia_com_power_battery_indicator_active",{ placer = inst,call_back = call_back })
            TheWorld:PushEvent("loramia_com_power_device_indicator_active",{ placer = inst,call_back = call_back })
        -------------------------------------------------------------------
        --- 创建自己的指示圈圈
            local fx = inst:SpawnChild("loramia_sfx_dotted_circle_client")
            fx:Hide()
            fx:PushEvent("Set",{
                pt = Vector3(0,0,0),
                radius = TRAP_RADIUS,
            })
            fx.AnimState:SetAddColour(0, 1, 1, 0)
            fx.AnimState:SetMultColour(0, 1, 1, 1)
            fx.AnimState:SetBloomEffectHandle("shaders/anim.ksh")
            inst:DoPeriodicTask(FRAMES*3,function()
                local in_battery_area_flag = false
                in_battery_area_flag = IsDeployerUnlockedTalent(ThePlayer)
                for k, flag in pairs(call_back.in_battery_area) do
                    if flag and not in_battery_area_flag then
                        in_battery_area_flag = true
                        break
                    end
                end
                if in_battery_area_flag then
                    fx.AnimState:SetAddColour(0, 1, 1, 0)
                    fx.AnimState:SetMultColour(0, 1, 1, 1)
                else
                    fx.AnimState:SetAddColour(1, 0, 0, 0)
                    fx.AnimState:SetMultColour(1, 0, 0, 1)
                end
                fx:Show()
            end)
        -------------------------------------------------------------------
    end
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
return Prefab("loramia_building_swiftstrike_creation", fn, assets),
    Prefab("loramia_building_swiftstrike_creation_kit", item_fn, assets),
    MakePlacer("loramia_building_swiftstrike_creation_kit_placer", "loramia_building_swiftstrike_creation", "loramia_building_swiftstrike_creation", "down_loop", nil, nil, nil, nil, nil, nil, placer_postinit_fn, nil, nil)
